package com.jeecg.mvc;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeecg.mvc.action.LoginAction;
import com.jeecg.mvc.form.RegisterForm;
import com.jeecg.mvc.lisenter.LoadMappingThread;
import com.jeecg.mvc.util.ConvertObject;
import com.jeecg.mvc.util.ResourceLoader;

public class ControlServlet  extends HttpServlet  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		System.out.println("启动加载struts的xml配置");
		LoadMappingThread load = new LoadMappingThread();
		new Thread(load).start();
		
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		ResourceLoader rl = new ResourceLoader();
		Map<String, Map<String, String>> map = rl.getMap();		
		Map<String, String> formActionMap = map.get(req.getParameter("formbean"));
		String formClass = null;
		String actionClass = null;
		for (String key : formActionMap.keySet()) {  
			   formClass = key;
			   actionClass = formActionMap.get(key);
	    }  
		
		
		@SuppressWarnings("unused")
		ActionForm form =  ConvertObject.getActionForm(req,formClass);
		System.out.println(form.getClass().getName());
		try {
			  Action  action = (Action)ConvertObject.getObject(actionClass);		
			  //返回页面字符串
			  String resultStr = action.execute(form);
			  req.getRequestDispatcher("/"+resultStr+".jsp").forward(req,resp); 
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
 	}

	
}


















